package com.cskaoyan.service;

import com.cskaoyan.bean.marketingmanagementbean.AdData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 创建日期: 2022/05/05 20:38
 *
 * @author liutao
 */
@Service
public class AdServiceImpl implements AdService{

    @Autowired
    MarketAdMapper marketAdMapper;

    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    public AdData query(BaseParam param, String name, String content) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        // List<User> list = userMapper.select(sort,order,username);

        //查询
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (content != null && !"".equals(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        marketAdExample.setOrderByClause(sort + " " + order);
        List<MarketAd> marketAdList = marketAdMapper.selectByExample(marketAdExample);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<MarketAd> pageInfo = new PageInfo<>(marketAdList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return AdData.data(total,pages,limit,page,marketAdList);
    }

    @Override
    public void delete(MarketAd marketAd) {
        int i = marketAdMapper.deleteByPrimaryKey(marketAd.getId());
    }

    @Override
    public void createStorage(MarketStorage marketStorage) {
        int i = marketStorageMapper.insert(marketStorage);
    }

    @Override
    public void createAd(MarketAd marketAd) {
        int insert = marketAdMapper.insertSelective(marketAd);
    }

    @Override
    public void adUpdate(MarketAd marketAd) {
        int i = marketAdMapper.updateByPrimaryKey(marketAd);
    }
}
